<?php

use common\models\User;
use yii\helpers\Html;
use yii\grid\GridView;
use common\widgets\ajaxbtn\Confirm;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\search\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">


<?php 
$list = Yii::$app->authManager->getRoles();
$roleList = [];
foreach ($list as $key=>$roleObj){
    $roleList[$key] = $key;
}
?>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?php echo Html::a('新增用户', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?php echo GridView::widget([
        'tableOptions' => ['class' => 'table table-striped table-bordered table-hover2'],
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'options' => [
            'class' => 'grid-view table-responsive'
        ],
        'columns' => [
            'id',
            'username',
            'email:email',
            [
                'attribute'=>'status',
                'format' => 'raw',
                'value' => function($model){
                    $str = '';
                    if($model->status == User::STATUS_ACTIVE){
                        $str = '<span style="color:green;">可使用</span>';
                    }elseif ($model->status == User::STATUS_DELETED){
                        $str = '<span style="color:red;">已禁用</span>';
                    }
                    return $str;
                },
                'filter' => Html::activeDropDownList($searchModel,'status',[User::STATUS_ACTIVE=>'可使用',User::STATUS_DELETED=>'已禁用'],['prompt'=>'全部','class'=>'form-control'])
            ],
            [
                'attribute' => 'role_type',
                'label' => '角色',
                'value' => function($model){
                    $str = '';
                    foreach ($model->roles as $role){
                        if(!empty($str)){
                            $str .= '、';
                        }
                        $str .= $role->item_name;
                    }
                    return $str;
                },
                'filter' => Html::activeDropDownList($searchModel,'role_type',$roleList,['prompt'=>'全部','class'=>'form-control'])
            ],
            [
                'label'=>'操作',
                'format' => 'raw',
                'value'=> function($model){
                    $str = '';
                    if($model->status == User::STATUS_ACTIVE){    //[可使用]状态下，可进行禁用处理
                        $confirmUrl = Confirm::widget(['url'=>Url::to(['set-status','id'=>$model->id]),'tip'=>'确定禁用该帐号？']);
                        $str .= '<a href="javascript:'.$confirmUrl.';" target="_self">禁用</a>';
                    
                    }elseif ($model->status == User::STATUS_DELETED){  //[禁用]状态下，可进行恢复处理
                        $confirmUrl = Confirm::widget(['url'=>Url::to(['set-status','id'=>$model->id]),'tip'=>'确定恢复该帐号？']);
                        $str .= '<a href="javascript:'.$confirmUrl.';" target="_self">恢复</a>';
                    }
                    return $str;
                }
            ],
            [
                'attribute' => 'created_at',
                'value' => function ($model){
                    return date('Y-m-d',$model->created_at);
                }
            ],
            [
                'attribute' => 'updated_at',
                'value' => function ($model){
                    return date('Y-m-d',$model->updated_at);
                }
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                //'template' => '{view} {update} {delete}',
                'template' => '{view} {update}',
                /* 'buttons' => [
                    'delete'=>function ($url, $model, $key) {
                        $confirmUrl = Confirm::widget(['url'=>Url::to(['delete','id'=>$model->id]),'tip'=>'确定删除该信息？']);
                        $str .= '<a href="javascript:'.$confirmUrl.';" target="_self"><span class="glyphicon glyphicon-trash"></span></a>';
                        return $str;
                    }
                ] */
            ],
            
        ],
    ]); ?>

</div>
